package com.comparyinfo.servlet;

import com.comparyinfo.entity.Shangpin;
import com.comparyinfo.entity.Xinwen;
import com.comparyinfo.service.ShangpinService;
import com.comparyinfo.service.XinwenService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ShangpinServlet",urlPatterns = "/ShangpinServlet")
public class ShangpinServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("UTF-8");
        ShangpinService service=new ShangpinService();
        if(method.equals("getAll")){//列表
            Shangpin obj=new Shangpin();
            //获取列表数据
            List<Shangpin> shangpinList=service.getListAll(obj);
            // 将结果存储到request中
            request.setAttribute("shangpinList", shangpinList);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/showProductList.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("list")){//列表
            Shangpin obj=new Shangpin();
            //获取列表数据
            List<Shangpin> shangpinList=service.getListAll(obj);
            // 将结果存储到request中
            request.setAttribute("shangpinList", shangpinList);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/manageProduct.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("add")){//新增
            String serialNumber=request.getParameter("serialnumber");
            String name=request.getParameter("name");
            String brand=request.getParameter("brand");
            String model=request.getParameter("model");
            String price=request.getParameter("price");
            String picture=request.getParameter("picture");
            String description=request.getParameter("description");
            List<Object> objects=new ArrayList<>();
            objects.add(serialNumber);
            objects.add(name);
            objects.add(brand);
            objects.add(model);
            objects.add(price);
            objects.add(picture);
            objects.add(description);
            service.add(objects);//新增
            response.sendRedirect("/ShangpinServlet?method=list");
        }else if(method.equals("up")){//修改
            response.setContentType("text/json;charset=utf-8");
            String serialNumber=request.getParameter("serialnumber");
            String name=request.getParameter("name");
            String brand=request.getParameter("brand");
            String model=request.getParameter("model");
            String price=request.getParameter("price");
            String picture=request.getParameter("picture");
            String description=request.getParameter("description");
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(serialNumber);
            objects.add(name);
            objects.add(brand);
            objects.add(model);
            objects.add(price);
            objects.add(picture);
            objects.add(description);
            objects.add(id);
            service.up(objects);//修改
            response.sendRedirect("/ShangpinServlet?method=list");
        }else if(method.equals("get")){//获取当前信息
            String id=request.getParameter("id");
            Shangpin shangpin=service.getId(id);
            // 将结果存储到request中
            request.setAttribute("shangpin", shangpin);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/updateProduct.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("info")){//获取当前信息
            String id=request.getParameter("id");
            Shangpin shangpin=service.getId(id);
            // 将结果存储到request中
            request.setAttribute("shangpin", shangpin);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/detailProduct.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("del")){//删除
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(id);
            service.del(objects);//删除
            response.sendRedirect("/ShangpinServlet?method=list");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
